package com.gupao.alg.alg9;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Topic3_2_102_Level_Travel_Element {

    public static void main(String[] args) {
        BinaryTree bTree = new BinaryTree();
        bTree.root = bTree.buildTreeByArray();
        int testMethod = 3;
        List<Integer> res = null;
        List<Double> res2 = null;
        switch (testMethod) {
            case 1://LeetCode 515 在每个树行中找最大值
                res = largestValues(bTree.root);
                System.out.println(res);
                break;
            case 2://LeetCode 637 在每个树行中找平均值
                res2 = averageOfLevels(bTree.root);
                System.out.println(res2);
                break;
            case 3://LeetCode 199 二叉树的右视图
                res = rightSideView(bTree.root);
                System.out.println(res);
                break;
        }
    }

    /**
     * LeetCode 515 在每个树行中找最大值
     *
     * @param root
     * @return
     */
    public static List<Integer> largestValues(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        LinkedList<TreeNode> linkedList = new LinkedList<>();
        linkedList.add(root);
        while (linkedList.size() > 0) {
            int size = linkedList.size();
            int max = 0;
            for (int i = 0; i < size; i++) {
                TreeNode remove = linkedList.remove();
                if(remove.left != null) {
                    linkedList.add(remove.left);
                }
                if(remove.right != null) {
                    linkedList.add(remove.right);
                }
                if(i == 0 || remove.val > max) {
                    max = remove.val;
                }
            }
            list.add(max);
        }
        return list;
    }

    /**
     * LeetCode 637 在每个树行中找平均值
     *
     * @param root
     * @return
     */
    public static List<Double> averageOfLevels(TreeNode root) {
        List<Double> list = new ArrayList<>();
        LinkedList<TreeNode> linkedList = new LinkedList<>();
        linkedList.add(root);
        while (linkedList.size() > 0) {
            int size = linkedList.size();
            double avl = 0;
            for (int i = 0; i < size; i++) {
                TreeNode remove = linkedList.remove();
                if(remove.left != null) {
                    linkedList.add(remove.left);
                }
                if(remove.right != null) {
                    linkedList.add(remove.right);
                }
                avl += remove.val;
            }
            list.add(avl / size);
        }
        return list;
    }

    /**
     * LeetCode 199 二叉树的右视图
     *
     * @param root
     * @return
     */
    public static List<Integer> rightSideView(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        LinkedList<TreeNode> linkedList = new LinkedList<>();
        linkedList.add(root);
        while (linkedList.size() > 0) {
            int size = linkedList.size();
            for (int i = 0; i < size; i++) {
                TreeNode remove = linkedList.remove();
                if(remove.left != null) {
                    linkedList.add(remove.left);
                }
                if(remove.right != null) {
                    linkedList.add(remove.right);
                }
                if(i == size - 1) {
                    list.add(remove.val);
                }
            }
        }
        return list;
    }
}

